package com.gym.myshop.web.controller;

import com.gym.myshop.common.Const;
import com.gym.myshop.entity.Product;
import com.gym.myshop.services.ProductServices;
import com.gym.myshop.services.impl.ProductServicesImpl;
import com.gym.myshop.util.StrUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author ming
 * 2021/11/3
 * description:分页控制
 */
@WebServlet("/pro")
public class ProductController extends BaseServlet {
    private ProductServices ps = new ProductServicesImpl();


    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:使用商品主键获取商品详情数据
     */
    public String getProById(HttpServletRequest request, HttpServletResponse response) {
        Integer pid = StrUtils.toInt(request.getParameter("pid"));
        Product pro = ps.getProById(pid);
        request.setAttribute("goods",pro );
        return Const.FORWARD+"goodsDetail.jsp";
    }


    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:获取分类商品的分页数据
     */
    public String getProByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer tid = StrUtils.toInt(request.getParameter("tid"));
        Integer pageIndex = StrUtils.toInt(request.getParameter("pageIndex"));
        //访问业务层获取数据 获取集合
        Map<String, Object> maps = ps.getProByPage(tid, pageIndex, 8);
        //前端需要商品分类组件
        request.setAttribute("maps", maps);
        return Const.FORWARD + "goodsList.jsp";
    }




}
